package baseclass.b_stackQueue;

import java.util.Stack;

/**
 * 栈实现队列
 * @date 2020/2/27 17:57
 */
public class Code06_StackToQueue {
    private Stack<Integer> pushStack;
    private Stack<Integer> popStack;

    public Code06_StackToQueue() {
        this.pushStack = new Stack<>();
        this.popStack = new Stack<>();
    }
    public void offer(int e){
        pushStack.push(e);
    }
    public int poll(){
        update();
        return popStack.pop();
    }

    /**
     * 什么时候才将pushStack的元素更新到popStack？
     * 1、popStack必须为空
     * 2、pushStack必须把元素都倒满给popStack
     */
    public void update(){
        if(!popStack.isEmpty()) return;
        System.out.println("发生update");
        while (!pushStack.isEmpty()){
            popStack.push(pushStack.pop());
        }
    }

    public boolean isEmpty(){
        return pushStack.isEmpty() && popStack.isEmpty();
    }

    public static void main(String[] args) {
        Code06_StackToQueue queue = new Code06_StackToQueue();
        for (int i = 1; i < 11; i++) {
            queue.offer(i);
        }
        while (!queue.isEmpty()){
            System.out.println(queue.poll());
        }
    }
}
